<?php
/**
 * @title 71. 简化路径 - 栈
 * @author start2004
 */

class Solution {

    /**
     * @param String $path
     * @return String
     */
    function simplifyPath($path) {
        /**
         * @since 2020-07-30 分隔数组
         */
        $paths = explode("/", $path);

        /**
         * @since 2020-07-30 结果集
         */
        $result = [];

        /**
         * @since 2020-07-30 栈处理
         */
        foreach ($paths as $folder){
            if($folder == "" OR $folder == "."){
                continue;
            } else {}

            /**
             * @since 2020-07-30 出栈
             */
            if($folder == ".."){
                array_pop($result);
            } else {
                array_push($result, $folder);
            }
        }

        /**
         * @return
         */
        return "/". implode("/", $result);
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.30_6.php
 */
$datas = [
    "/home/",
    "/../",
    "/home//foo/",
    "/a/./b/../../c/",
    "/a/../../b/../c//.//",
    "/a/b////c/d//././/..",
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->simplifyPath($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
